import base64
from func import imgs
from PyQt5.QtWidgets import QVBoxLayout, QLabel, QLineEdit, QDialog, QHBoxLayout, QFrame, \
    QPushButton, QMessageBox, QFileDialog
from PyQt5.QtGui import QPixmap
from config import Text_config,input_config,button_config
from PyQt5.QtCore import Qt
class UpdateUser(QDialog):
    def __init__(self,parent=None):
        super(UpdateUser,self).__init__(parent)
        self.parent = parent
        self.client=self.parent.client
        self.user_name=self.parent.user_name
        self.email=self.parent.email
        self.password=self.parent.password
        self.avatar=self.parent.avatar
        self.resize(400,500)
        self.setWindowTitle('修改个人信息')
        self.is_p=False
        self.initUI()
    def initUI(self):
        main_layout = QVBoxLayout(self)
        main_layout.setSpacing(20)

        frame=QFrame()
        main_layout.addWidget(frame)
        layout1=QVBoxLayout(frame)
        self.img_label=QLabel()
        img=QPixmap()
        img.loadFromData(self.avatar)
        img = img.scaled(200, 200, Qt.KeepAspectRatio)
        self.img_label.setPixmap(img)
        self.img_label.setAlignment(Qt.AlignCenter)
        layout1.addWidget(self.img_label)
        btn_img=QPushButton()
        layout1.addWidget(btn_img,alignment=Qt.AlignCenter)
        btn_img.setText('更换头像')
        btn_img.setFixedSize(100,40)

        label=QLabel()
        label.setText('用户名')
        label.setStyleSheet(Text_config.text_style2)
        self.user_name_input=QLineEdit()
        self.user_name_input.setText(self.user_name)
        self.user_name_input.setPlaceholderText('请输入修改后的用户名')
        self.user_name_input.setStyleSheet(input_config.input_style1)
        self.user_name_input.setFixedHeight(30)
        main_layout.addWidget(label)
        main_layout.addWidget(self.user_name_input)

        label=QLabel()
        label.setText('用户邮箱')
        label.setStyleSheet(Text_config.text_style2)
        self.user_email_input=QLineEdit()
        self.user_email_input.setText(self.email)
        self.user_email_input.setPlaceholderText('请输入修改后的用户邮箱')
        self.user_email_input.setStyleSheet(input_config.input_style1)
        self.user_email_input.setFixedHeight(30)
        main_layout.addWidget(label)
        main_layout.addWidget(self.user_email_input)

        frame=QFrame()
        main_layout.addWidget(frame)
        layout1=QVBoxLayout(frame)
        label=QLabel()
        label.setText('用户密码')
        label.setStyleSheet(Text_config.text_style2)
        layout1.addWidget(label)
        layout2=QHBoxLayout()
        layout1.addLayout(layout2)
        self.old_password=QLineEdit()
        self.old_password.setPlaceholderText('请在此处输入你的旧密码')
        self.old_password.setStyleSheet(input_config.input_style1)
        self.old_password.setEchoMode(QLineEdit.Password)
        layout2.addWidget(self.old_password)
        self.btn_pwd=QPushButton()
        self.btn_pwd.setText('提交')
        layout2.addWidget(self.btn_pwd)
        self.new_password=QLineEdit()
        self.new_password.setPlaceholderText('输入修改后的密码')
        self.new_password.setEchoMode(QLineEdit.Password)
        self.new_password.setStyleSheet(input_config.input_style1)
        self.new_password.setEnabled(False)
        layout1.addWidget(self.new_password)

        layout1=QHBoxLayout()
        main_layout.addLayout(layout1)
        btn_submit=QPushButton()
        btn_submit.setText('提交')
        btn_submit.setStyleSheet(button_config.button_style2)
        layout1.addWidget(btn_submit)
        btn_submit.setFixedHeight(40)

        btn_exit=QPushButton()
        btn_exit.setText('退出')
        btn_exit.setStyleSheet(button_config.button_style4)
        layout1.addWidget(btn_exit)
        btn_exit.setFixedHeight(40)

        btn_img.clicked.connect(self.img_update)
        self.btn_pwd.clicked.connect(self.change_password)
        btn_submit.clicked.connect(self.submit)
        btn_exit.clicked.connect(self.exit)

    def img_update(self):
        self.file_path=QFileDialog.getOpenFileName(self,'打开','./','(*.png *.jpg *.jpeg *.bmp)')[0]
        with open(self.file_path,'rb') as f:
            avatar=f.read()
        self.avatar = imgs.resize_(avatar, 128, 128)
        img=QPixmap()
        img.loadFromData(self.avatar)
        img = img.scaled(200, 200, Qt.KeepAspectRatio)
        self.img_label.setPixmap(img)

    def change_password(self):
        if self.old_password.text() != self.password:
            QMessageBox.critical(None,'错误','输入的密码不正确')
        else:
            self.is_p=True
            self.new_password.setEnabled(True)
            self.btn_pwd.setEnabled(False)

    def submit(self):
        user_name=self.user_name_input.text()
        email=self.user_email_input.text()
        if self.is_p:
            password=self.new_password.text()
        else:
            password=self.password
        if not user_name or not email or not password:
            QMessageBox.critical(None,'错误','请将信息填写完整')
            return
        if len(password) < 8:
            QMessageBox.critical(None,'错误','密码至少为8位')
            return
        if self.avatar == self.parent.avatar and self.user_name==user_name and self.email==email and self.password==password:
            QMessageBox.critical(None,'错误','你还没有修改任何信息')
        else:
            avatar = base64.b64encode(self.avatar).decode('utf-8')
            parser={
                'old_username':self.user_name,
                'new_username':user_name,
                'email':email,
                'password':password,
                'avatar':avatar
            }
            data=self.client.update_user(parser)
            if data.get('code')==200:
                QMessageBox.information(None,'提示',data.get('msg'))
                self.parent.user_name=user_name
                self.parent.email=email
                self.parent.password=password
                self.parent.avatar=self.avatar
            else:
                QMessageBox.critical(None,'错误',data.get('msg'))



    def exit(self):
        self.close()
        self.accept()

    def accept(self):
        super().accept()